package com.coin.controller;

import com.coin.domain.*;
import com.coin.model.R;
import com.coin.service.UserFavoriteMarketService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

/**
 * @Auther: 李 力
 * @Date: 2025/6/23
 * @Description: com.coin.controller
 * @version: 1.0
 */
@RestController
@RequestMapping("/userFavorites")
@Api(tags = "用户市场收藏控制器")
public class UserFavoriteMarketController {

    @Autowired
    private UserFavoriteMarketService userFavoriteMarketService;

    /**
     * @Description: 用户收藏某个市场
     * @Author: 李 力
     * @Date: 2025/6/23
     * @Param:
     * @return: R
     */
    @PostMapping("/addFavorite")
    @ApiOperation(value = "用户收藏某个市场")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "market", value = "market的交易对和类型")
    })
    public R addFavorite(@RequestBody Market market) {
        Long userId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        boolean isOk = userFavoriteMarketService.saveMarket(userId, market);
        return isOk ? R.ok() : R.fail("收藏失败");
    }

    /**
     * @Description: 用户取消收藏某个市场
     * @Author: 李 力
     * @Date: 2025/6/23
     * @Param:
     * @return: R
     */
    @DeleteMapping("/{symbol}")
    @ApiOperation(value = "用户取消收藏某个市场")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "symbol", value = "交易对")
    })
    public R deleteUserFavoriteMarket(@PathVariable("symbol") String symbol) {
        Long userId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        boolean isOk = userFavoriteMarketService.deleteUserFavoriteMarket(symbol, userId);
        return isOk ? R.ok() : R.fail("取消收藏");
    }
}
